package com.chaofan.authsso.client;

import com.chaofan.authsso.client.common.SsoRestfulMessage;
import com.chaofan.authsso.client.properties.SsoRegisterProperties;
import com.chaofan.authsso.client.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpSession;

/**
 * Author: Chaofan
 * Date: 2020/6/8 16:59
 * email: chaofan2685@qq.com
 */
public class AuthSsoClient {

    @Autowired
    HttpSession session;

    @Autowired
    SsoRegisterProperties properties;

    /**
     * 退出登录
     * @return
     */
    public boolean logout(){
        Object token = session.getAttribute(properties.getTokenName());
        session.invalidate();
        if (token != null){
            SsoRestfulMessage restfulMessage = TokenUtil.logout(properties.getHost(),token.toString());
            return restfulMessage.successful();
        }
        return false;
    }

    /**
     * 获取AuthSso的token
     * @return
     */
    public String token(){
        Object token = session.getAttribute(properties.getTokenName());
        if (token != null){
            return token.toString();
        }else {
            return null;
        }
    }

}
